package simplehbase;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.HBaseConfiguration;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.client.ConnectionFactory;

import java.io.IOException;

public class CreateTableTest {

    /**
     * 创建myuser表，有两个列族f1 f2
     * **/
    public static void createTable() throws IOException {
        Configuration configuration = HBaseConfiguration.create();
        configuration.set("hbase.zookeeper.quorum","node01:2181,node02:2181,node03:2181");

        Connection connection = ConnectionFactory.createConnection(configuration);

        //操作：建表、删除表、修改表 -> 管理员：创建管理员对象
        Admin admin = connection.getAdmin();

        //添加表名信息
        HTableDescriptor myuser = new HTableDescriptor(TableName.valueOf("myuser"));
        //给表添加列族
        myuser.addFamily(new HColumnDescriptor("f1"));
        myuser.addFamily(new HColumnDescriptor("f2"));

        admin.createTable(myuser);

        admin.close();
        connection.close();
    }

    public static void main(String[] args) throws Exception {
        createTable();
    }
}


